package wb.is.besti.edu.cd.javalibgame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by asus on 2017/12/5.
 */

public class Card extends FrameLayout {
    private int num = 0;
    private TextView label;
    private View background;
    private MyView myView;


    public Card(@NonNull Context context) {
        super(context);
        LayoutParams lp = null;
        myView = new MyView(getContext());
        background = new View(getContext());

        lp = new LayoutParams(-1,-1);
        lp.setMargins(10,10,0,0);
        background.setBackgroundColor(0x33ffffff);



        addView(background,lp);
        label = new TextView(getContext());

        label.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/typesc.ttf"));
        label.setTextSize(20);
        label.setGravity(Gravity.CENTER);
        lp = new LayoutParams(-1,-1);
        lp.setMargins(10,10,0,0);

        addView(label,lp);
        init();
        setNum(0);
    }

    public Card(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private final RectF roundRect = new RectF();
    private float rect_adius = 12;
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();

    private void init() {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);

        float density = getResources().getDisplayMetrics().density;
        rect_adius = rect_adius * density;
    }

    public void setCorner(float adius) {
        rect_adius = adius;
        invalidate();
    }
    public void setNum(int num) {
        this.num = num;

        if (num<=0) {
            label.setText("");
        }else{
            label.setText(num+"");
        }

        switch (num) {
            case 0:
                label.setBackgroundColor(0x00000000);

                break;
            case 2:
                label.setBackgroundColor(0xffeee4da);

                break;
            case 4:
                label.setBackgroundColor(0xffede0c8);
                break;
            case 8:
                label.setBackgroundColor(0xfff2b179);
                break;
            case 16:
                label.setBackgroundColor(0xfff59563);
                break;
            case 32:
                label.setBackgroundColor(0xfff67c5f);
                break;
            case 64:
                label.setBackgroundColor(0xfff65e3b);
                break;
            case 128:
                label.setBackgroundColor(0xffedcf72);
                break;
            case 256:
                label.setBackgroundColor(0xffedcc61);
                break;
            case 512:
                label.setBackgroundColor(0xffedc850);
                break;
            case 1024:
                label.setBackgroundColor(0xffedc53f);
                break;
            case 2048:
                label.setBackgroundColor(0xffedc22e);
                break;
            default:
                label.setBackgroundColor(0xff3c3a32);
                break;
        }
    }
    public int getNum() {
        return num;
    }
    public boolean equals(Card o) {
        return getNum()==o.getNum();
    }
    protected Card clone(){
        Card c= new Card(getContext());
        c.setNum(getNum());
        return c;
    }

    public TextView getLabel() {
        return label;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        roundRect.set(0,0, w, h);

    }

    @Override
    public void draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }
}
